package com.topscomm.mis.cbb.pm.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.topscomm.pub.util.ConvertUtil;

import lombok.Data;

/**
 * @description:树形节点模型
 * @param
 * @author: hudeyong
 * @date: 2023-06-15
 * @modify:
 */
@Data
public class TaskTreeModel implements Serializable {
    private static final long serialVersionUID = 4013193970046502756L;
    protected String id;
    protected String index;
    protected String code;
    protected String pcode;
    protected String name;
    protected String label;
    protected String responsibleusername;
    protected String planbegintime;
    protected String planendtime;
    protected String status;
    protected String dispstatus;
    protected String realbegintime;
    protected String realendtime;
    protected String pretaskname;
    protected int source;
    protected int level;

    protected Map<String, Object> treeFullData;
    protected List<TaskTreeModel> children;

    public TaskTreeModel() {
    }

    /**
     * @description:构建树模型
     * @param treeNodes:
     * @param isAddRoot:
     * @param rootName:
     * @author: hudeyong
     * @date: 2023-06-15
     * @modify:
     */
    public List<TaskTreeModel> buildTree(List<Map<String, Object>> treeNodes, boolean isAddRoot, String rootName) {
        // 如果支持根节点，则增加默认根节点
        if (isAddRoot == true) {
            Map<String, Object> rootMap = new HashMap<String, Object>();
            rootMap.put("code", "0");
            rootMap.put("pcode", "-1");
            rootMap.put("name", rootName);
            treeNodes.add(rootMap);
        }
        // 将参数Map转换为无上下级的树节点
        List<TaskTreeModel> tempTreeModels = new ArrayList<TaskTreeModel>();
        for (Map<String, Object> treeNode : treeNodes) {
            TaskTreeModel treeModel = new TaskTreeModel();
            treeModel.copyValues(treeNode);
            tempTreeModels.add(treeModel);
        }
        // 构建上下级的树节点
        List<TaskTreeModel> treeModels = new ArrayList<TaskTreeModel>();
        // 构建节点
        for (TaskTreeModel tempTreeModel : tempTreeModels) {
            if (tempTreeModel.getCode().length() <= 1) {
                treeModels.add(tempTreeModel);
                continue;
            }
            boolean haveParent = false;
            for (TaskTreeModel parentTreeModel : tempTreeModels) {
                if (ConvertUtil.convertToString(tempTreeModel.getPcode())
                        .equals(ConvertUtil.convertToString(parentTreeModel.getCode()))) {
                    if (parentTreeModel.getChildren() == null) {
                        parentTreeModel.setChildren(new ArrayList<TaskTreeModel>());
                    }
                    parentTreeModel.getChildren().add(tempTreeModel);
                    haveParent = true;
                    break;
                }
            }
            if (haveParent == false) {
                treeModels.add(tempTreeModel);
            }
        }
        return treeModels;
    }

    /**
     * @description:
     * @param map
     * @author: hudeyong
     * @date: 2023-06-15
     * @modify:
     */
    public void copyValues(Map<String, Object> map) {
        this.id = ConvertUtil.convertToString(map.get("id"));
        this.code = ConvertUtil.convertToString(map.get("code"));
        this.pcode = ConvertUtil.convertToString(map.get("parentcode"));
        this.name = ConvertUtil.convertToString(map.get("name"));
        this.label = ConvertUtil.convertToString(map.get("name"));
        this.responsibleusername = ConvertUtil.convertToString(map.get("responsibleusername"));
        this.planbegintime = ConvertUtil.convertToString(map.get("planbegintime"));
        this.planendtime = ConvertUtil.convertToString(map.get("planendtime"));
        this.status = ConvertUtil.convertToString(map.get("status"));
        this.dispstatus = ConvertUtil.convertToString(map.get("dispstatus"));
        this.realbegintime = ConvertUtil.convertToString(map.get("realbegintime"));
        this.realendtime = ConvertUtil.convertToString(map.get("realendtime"));
        this.pretaskname = ConvertUtil.convertToString(map.get("pretaskname"));
        this.source = ConvertUtil.convertToInt(map.get("source"));
        this.level = ConvertUtil.convertToInt(map.get("level"));
        this.treeFullData = map;
    }
}
